#!/bin/bash

params=$@

if [ $# -eq 0 ] ; then
  echo "$0: Missing params" ; 
  echo "Usage: $0 [all='commit message..'] ..." ;
  exit 1 ;
else
  for p in $params ; do
    set -- `echo $params | tr '=' ' '`
    if [ -z "$2" ] ; then
      echo "commit message is empty" 
      exit 1 ;
    fi ;
  done ;
fi ;
 
fullpath=`pwd -P .`
cd $fullpath


echo "--------------------------------------------------"
echo "Ready to git push: $params"
echo "--------------------------------------------------"

msg=""
for p in $params ; do
  set -- `echo $p | tr '=' ' '` 
  k=$1 
  v=$2 
  msg=$v
  if [ $k == "all" ] ; then
    for f in */ ; do
      if [ -d "$f" ] ; then
      	echo "------------------------< $f >"
        cd $f
		git add .
	    	git commit -m $v
	    	branch=`git rev-parse --abbrev-ref HEAD`
	    	git push origin $branch
	    	cd $fullpath ;
      fi ;
    done ;
  elif [ -d "$k" ] ; then
    cd $k 
    git add .
    git commit -m $v
    branch=`git rev-parse --abbrev-ref HEAD`
    git push origin $branch
    cd $fullpath ;
  else
    echo "$k directory not exists!"
    exit 1 ;
  fi 
done ;

echo "------------------------< $fullpath >"
cd $fullpath
git add .
git commit -m $msg
branch=`git rev-parse --abbrev-ref HEAD`
git push origin $branch
echo "--------------------------------------------------"




